//
//  ViewController.m
//  WJSinaHomeMenu
//
//  Created by tqh on 2019/5/16.
//  Copyright © 2019 tqh. All rights reserved.
//

#import "ViewController.h"
#import "YNPageViewController.h"
#import "HexColors.h"
#import "BaseTableViewVC.h"
#import "BaseCollectionViewVC.h"
#import "MouoSinaMenuView.h"
#import "MouoFont.h"

@interface ViewController ()<YNPageViewControllerDataSource,YNPageViewControllerDelegate,MouoSinaMenuViewDelegate>

@property (nonatomic,strong) YNPageViewController *pageController;//分页控制器
@property (nonatomic,strong) MouoSinaMenuView *menuView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64);
    [self createPageControllerWithArray:@[@"要闻",@"河北",@"财经",@"娱乐",@"体育",@"社会",@"NBA",@"视频",@"汽车",@"图片",@"科技",@"军事",@"国际",@"数码",@"星座",@"电影",@"时尚",@"文化",@"游戏",@"教育",@"动漫",@"政务",@"纪录片",@"房产",@"佛学",@"股票",@"理财"] index:0];
    [self.view addSubview:self.menuView];
    NSArray *arr1 = @[@"要闻",@"河北",@"财经",@"娱乐",@"体育",@"社会",@"NBA",@"视频",@"汽车",@"图片",@"科技",@"军事",@"国际",@"数码",@"星座",@"电影",@"时尚",@"文化",@"游戏",@"教育",@"动漫",@"政务",@"纪录片",@"房产",@"佛学",@"股票",@"理财"];
    NSArray *arr2 = @[@"有声",@"家居",@"电竞",@"美容",@"电视剧",@"搏击",@"健康",@"摄影",@"生活",@"旅游",@"韩流",@"探索",@"综艺",@"美食",@"育儿"];
    [self.menuView addMenuWithInUseTitle:arr1 unUseTitle:arr2];
    
}

- (void)createPageControllerWithArray:(NSArray *)array index:(NSInteger)index {
    YNPageConfigration *configration = [YNPageConfigration defaultConfig];
    configration.scrollMenu = YES;
    configration.showAddButton = YES;
    configration.selectedItemColor = [UIColor colorWithHexString:@"333333"];
    configration.normalItemColor = [UIColor colorWithHexString:@"#9B9B9B"];
    configration.lineHeight = 0;
    
    NSMutableArray *controllerArray = [NSMutableArray array];
    for (int i = 0; i<array.count; i++) {
        BaseTableViewVC *base = [[BaseTableViewVC alloc]init];
        [controllerArray addObject:base];
    }
    YNPageViewController *vc = [YNPageViewController pageViewControllerWithControllers:controllerArray titles:array  config:configration];
    vc.dataSource = self;
    vc.delegate = self;
    vc.pageIndex = index;
    self.pageController = vc;
    [vc addSelfToParentViewController:self];
    self.pageController = vc;
}

#pragma mark - <YNPageViewControllerDataSource>

- (UIScrollView *)pageViewController:(YNPageViewController *)pageViewController pageForIndex:(NSInteger)index {
    UIViewController *vc = pageViewController.controllersM[index];
    if ([vc isKindOfClass:[BaseTableViewVC class]]) {
        return [(BaseTableViewVC *)vc tableView];
    } else {
        return [(BaseCollectionViewVC *)vc collectionView];
    }
}

#pragma mark - <YNPageViewControllerDelegate>
- (void)pageViewController:(YNPageViewController *)pageViewController didAddButtonAction:(UIButton *)button {
    [self.view insertSubview:self.menuView aboveSubview:self.pageController.view];
    [self.menuView showMenuWithIndex:self.pageController.pageIndex];
}

#pragma mar - <MouoSinaMenuViewDelegate>

- (void)mouoSinaMenuViewDidDoneWithinUseTitles:(NSArray *)inUseTitles unUseTitles:(NSArray *)unUseTitles index:(NSInteger)index {
    [self.pageController removeSelfViewController];
    [self createPageControllerWithArray:inUseTitles index:index];
}

- (void)mouoSinaMenuViewDidIndex:(NSInteger)index {
    [self.pageController setSelectedPageIndex:index];
}

#pragma mark - 懒加载

- (MouoSinaMenuView *)menuView {
    if (!_menuView) {
        _menuView =[[MouoSinaMenuView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 380)];
        _menuView.delegate = self;
    }
    return _menuView;
}

@end
